// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

let CHESS_STATUS = {
    BLOCK: 0,//还没有翻开， 呈石头状态
    ALIVE: 1, //已经翻开，呈动物形态
    DEAD: 2, //已经死亡， 用作空位置，供其他动物移动
};

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        animalNode: {
            type: cc.Node,
            default: null
        },
        pedestalNode: {
            type: cc.Node,
            default: null
        },

        pedestalBorderNode: {
            type: cc.Node,
            default: null
        },

        tagNode: {
            type: cc.Node,
            default: null
        },
        unOpenNode: {
            type: cc.Node,
            default: null
        },

        leftSmokeNode: {
            type: cc.Node,
            default: null
        },

        rightSmokeNode: {
            type: cc.Node,
            default: null
        },

        eatSmokeNode: {
            type: cc.Node,
            default: null
        },

        unopenBorderNode: {
            type: cc.Node,
            default: null
        },

        moveTime: {
            type: cc.Float,
            default: 0.4,
            desc: "棋子移动时间"
        },

        eatAudio: {
            url: cc.AudioClip,
            default: null
        },

        openAudio: {
            url: cc.AudioClip,
            default: null
        },

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        cc.maxZIndex = 999;
    },

    init(type, name, belong, belongSelf) {
        this.type = type;
        this.name = name;
        this.belong = belong;
        this.status = CHESS_STATUS.BLOCK;
        this.alive = true;
        this.belongSelf = belongSelf;//是否是自己的棋子
    },

    //固定自己的棋子显示为蓝色
    showColor() {
        let showColor = "red";
        if (this.belongSelf) showColor = "blue";
        return showColor;
    },

    resetTagFrame() {
        let self = this;

        let url = "Texture/pieces/label-" + this.showColor() + "/label-" + this.showColor() + "-" + this.type;
        cc.loader.loadRes(url, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
            } else {
                if(self.tagNode) {
                   self.tagNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            }
        });
    },

    resetAnimalFrame() {
        let self = this;
        cc.loader.loadRes("Texture/pieces/animals/" + this.type, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
            } else {
                if(self.animalNode) {
                   self.animalNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            }
        });
    },

    resetPedestalFrame() {
        let self = this;
        cc.loader.loadRes("Texture/pieces/bases/base-" + this.showColor(), cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                cc.log(err);
            } else {
                if(self.pedestalNode) {
                    self.pedestalNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            }

        });
    },

    isOpened() {
        return this.status != CHESS_STATUS.BLOCK;
    },

    open(finishCallback) {
        let ani = this.node.getComponent(cc.Animation);

        ani.playAdditive("animal_appear");
        ani.once("finished", function () {
            this.unOpenNode.destroy();
            this.resetTagFrame();
            if (finishCallback) finishCallback();
        }.bind(this));

        this.status = CHESS_STATUS.ALIVE;

        this.resetAnimalFrame();
        this.resetPedestalFrame();

        // cc.mzPlayAudio(this.openAudio);

        cc.loader.loadRes("audio/" + this.type, cc.AudioClip, function (err, audio) {
            if (err) {
                cc.log(err);
            } else {
                cc.mzPlayAudio(audio);
            }

        });
    },

    choosed(c) {
        let self = this;
        let ani = this.node.getComponent(cc.Animation);
        if (c && !this.checked) {
            ani.playAdditive("chess_select");
        } else if (!c && this.checked) {
            ani.playAdditive("chess_unselect");
        }
        this.checked = c;
    },

    move(toX, toY, finishCallback) {
        let finishNum = 0;
        //移动的棋子层级最高，保证吃子的棋子不被遮挡
        cc.maxZIndex += 1
        this.node.setLocalZOrder(cc.maxZIndex);
        cc.log("zindex:" + cc.maxZIndex);
        var moveAction = cc.moveBy(this.moveTime, cc.p(toX - this.node.x, toY - this.node.y));
        let action = cc.sequence(moveAction, cc.callFunc(function () {
            let leftAni = this.leftSmokeNode.getComponent(cc.Animation);
            leftAni.playAdditive("move_smoke");
            leftAni.on("finished", function () {
                this.leftSmokeNode.getComponent(cc.Sprite).spriteFrame = null;
            }.bind(this));

            let rightAni = this.rightSmokeNode.getComponent(cc.Animation);
            rightAni.playAdditive("move_smoke");
            rightAni.on("finished", function () {
                this.rightSmokeNode.getComponent(cc.Sprite).spriteFrame = null;
            }.bind(this));

            finishNum++;
            if (finishCallback && finishNum == 2) {
                finishCallback();
            }
        }.bind(this)));
        this.node.runAction(action);

        let ani = this.node.getComponent(cc.Animation);
        ani.playAdditive("move_down");
        ani.once("finished", function () {
            finishNum++;
            if (finishCallback && finishNum == 2) {
                finishCallback();
            }
        });
        this.checked = false;
    },
    //死亡
    die(callback) {
        this.alive = false;
        // this.node.removeAllChildren(true);
        this.status = CHESS_STATUS.DEAD;

        let eatAni = this.node.getComponent(cc.Animation);
        eatAni.playAdditive("die");
        eatAni.once("finished", function () {
            this.node.removeAllChildren(true);
            if (callback) callback();
        }.bind(this));
    },

    //吃棋
    eat(callback) {
        let eatAni = this.node.getComponent(cc.Animation);
        eatAni.playAdditive("eat");
        eatAni.once("finished", function () {
            if (callback) callback();
        }.bind(this));

        cc.mzPlayAudio(this.eatAudio);
    },

    setBorder(color) {
        let self = this;
        if (color) {
            if (!this.isOpened()) {
                cc.loader.loadRes("Texture/pieces/piece-unopened-border-" + color, cc.SpriteFrame, function (err, spriteFrame) {
                    if (err) {
                        cc.log(err);
                    } else {
                        if(self.unopenBorderNode) {
                            self.unopenBorderNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                        }
                    }

                });
            } else {
                cc.loader.loadRes("Texture/pieces/bases/base-border-" + color, cc.SpriteFrame, function (err, spriteFrame) {
                    if (err) {
                        cc.log(err);
                    } else {
                        if(self.pedestalBorderNode) {
                            self.pedestalBorderNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                        }
                    }

                });
                this.pedestalNode
            }
        } else {
            self.unopenBorderNode.getComponent(cc.Sprite).spriteFrame = null;
            self.pedestalBorderNode.getComponent(cc.Sprite).spriteFrame = null;

        }


    }

    // destroy(){
    //     this.node.getChildByName("block").getComponent(cc.Sprite).spriteFrame = this.bomb;
    //     var self = this;
    //     setTimeout(() => {
    //         self.node.destroy();
    //     }, 300);
    // }

    // update (dt) {},
});
